<?php


namespace app\admin\controller\xsms;


use app\common\controller\Adminbase;
use think\Db;

class Timescope extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['is_del', '=', 0];
            $_list = Db::name('xsms_time')
                ->page($page, $limit)
                ->order('start_time')
                ->where($where)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $goods_num = Db::name('goods')->where([
                    ['ms_id', '=', $v['id']],
                    ['is_del', '=', 0]
                ])->count();
                $_list[$k]['goods_num'] = $goods_num;
            }
            $total = Db::name('xsms_time')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('xsms_time')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('xsms_time')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $insert_data = array(
                'start_time' => $data['start_time'],
                'delayed_time' => $data['delayed_time'],
                'create_time' => time(),
            );
            $stmt = Db::name('xsms_time')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $time_scope = array(
            '01:00', '02:00',
            '03:00', '04:00',
            '05:00', '06:00',
            '07:00', '08:00',
            '09:00', '10:00',
            '11:00', '12:00',
            '13:00', '14:00',
            '15:00', '16:00',
            '17:00', '18:00',
            '19:00', '20:00',
            '21:00', '22:00',
            '23:00', '00:00',
        );
        $this->assign([
            'time_scope' => $time_scope
        ]);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'start_time' => $data['start_time'],
                'delayed_time' => $data['delayed_time']
            );
            $stmt = Db::name('xsms_time')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('xsms_time')->find($id);
        $time_scope = array(
            '01:00', '02:00',
            '03:00', '04:00',
            '05:00', '06:00',
            '07:00', '08:00',
            '09:00', '10:00',
            '11:00', '12:00',
            '13:00', '14:00',
            '15:00', '16:00',
            '17:00', '18:00',
            '19:00', '20:00',
            '21:00', '22:00',
            '23:00', '00:00',
        );
        $this->assign([
            'time_scope' => $time_scope,
            'data' => $data
        ]);
        return $this->fetch();
    }

    public function goodseditc()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {

        }
        $this->assign([
            'business_id' => $id,
        ]);
        return $this->fetch();
    }


}